#pragma once
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include <io.h>
#include <direct.h>
#include <errno.h>
#include <algorithm>
#include "pic.h"
using namespace std;
using namespace cv;
using namespace ImgParse;

class decode {
public:
    void ConvertQRToBin(const string& inputVideoPath, const string& outputBinPath,
        const string& validityPath, int imageWidth, int imageHeight, int blockSize, int fps);

private:
    bool IsStartFrame(const cv::Mat& frame, int blockSize) const;
    std::vector<std::string> SplitVideoToFrames(const std::string& videoPath, const std::string& outputDir, int fps);
    std::vector<unsigned char> DecodeFramesToBinaryBits(const std::vector<std::string>& framePaths, int blockSize);
    std::vector<unsigned char> ReassembleBytesFromBits(const std::vector<unsigned char>& bits, int& errorCount, vector<bool>& errorFlags);
    bool SaveBytesToBin(const std::vector<unsigned char>& data, const std::string& outputPath);
    bool SaveErrorFlagsToBin(const std::vector<bool>& errorFlags, const std::string& outputPath);
    bool IsInPositionArea(int r, int c, int row, int col) const;
    std::vector<unsigned char> DecodeSingleImageBits(const cv::Mat& img, int blockSize);
    int MapBgrToColorIndex(const cv::Vec3b& bgr) const;
    int Checksum(const void* dataptr, unsigned short len) const;
    std::vector<unsigned char> BoolVectorToBytes(const std::vector<bool>& boolVec) const;
};